//輸入欄位過濾器
;(function(){
	var main;
	main = Filter = {
		saveSelectRange : function(o, sr){
			if (!sr instanceof main.createSelectRange){return;}
			o._selectionStart = sr.selectionStart;
			o._selectionEnd = sr.selectionEnd;
			o._value_old = o.value;
		},
		createSelectRange : function(o){
			this.selectionStart = 0;
			this.selectionEnd = 0;
			this.selectionRight = 0;
			if (typeof o.selectionStart == typeof 0)
			{
				this.selectionStart = o.selectionStart;
				this.selectionEnd = o.selectionEnd;
				this.selectionRight = o.value.length - o.selectionEnd;
			}
			else if (document.selection)
			{
				var sel_range = document.selection.createRange();
				var len = sel_range.text.length;
				
				sel_range.moveStart('character', -o.value.length);
				
				this.selectionStart = sel_range.text.length - len;
				this.selectionEnd = sel_range.text.length;
				this.selectionRight = o.value.length - sel_range.text.length;
			}
		},
		backSelectRange : function(o, sr, valueBack){
			if (!sr instanceof main.createSelectRange){return;}
			var value_old = o._value_old || o.value;
			var undef;
			if (typeof o.selectionStart == typeof 0)
			{
				if (valueBack && undef != o._selectionStart)
				{
					o.value = value_old;
					o.selectionStart = o._selectionStart;
					o.selectionEnd = o._selectionEnd;
				}
				else
				{
					o.selectionStart = sr.selectionStart;
					o.selectionEnd = o.value.length - sr.selectionRight;
					main.saveSelectRange(o, sr);
				}
			}
			else if (document.selection)
			{
				var sel_range = document.selection.createRange();
				if (valueBack && undef != o._selectionStart)
				{
					o.value = value_old;
					sel_range.move('character', o._selectionEnd);
					sel_range.moveStart('character', o._selectionStart - o._selectionEnd);
				}
				else 
				{
					if (sr.selectionStart != sr.selectionEnd)
					{
						sel_range.move('character', sr.selectionEnd);
						sel_range.moveStart('character', sr.selectionStart - sr.selectionEnd);
					}
					else if (sr.selectionRight)
					{
						if (o.tagName && 'input' == o.tagName.toLowerCase()) 
						{
							o.select();
							sel_range = document.selection.createRange();
						}
						sel_range.move('character', sr.selectionEnd);
					}
					else
					{
						sel_range.move('character', sr.selectionEnd);
					}
					main.saveSelectRange(o, sr);
				}
				sel_range.select();
			}
		},
		toNum : function(e)
		{
			var x = new main.createSelectRange(this);
			var valueBack;
			this.value = this.value.replace(/[^\d]/g, function(){valueBack = true; return '';});
			main.backSelectRange(this, x, valueBack);
		},
		// Natural Number
		toNNum : function(e)
		{
			var x = new main.createSelectRange(this);
			var valueBack;
			this.value = this.value.replace(/[^\d]|^0+(\d+)$/g, function(){valueBack = true; return (arguments[1]||'');});
			main.backSelectRange(this, x, valueBack);
		},
		toNegative : function(e)
		{
			var x = new main.createSelectRange(this);
			var valueBack, o = {};
			if (this.value.match(/^$/) || this.value.match(/^-\D+/))
			{
				this.value = '-0.0';
				x.selectionStart = 1;
				x.selectionEnd = this.value.length;
				valueBack = true;
			}
			else if (this.value.match(/^-$/))
			{
				this.value += '0.0';
				x.selectionEnd = this.value.length;
			}
			else if (this.value.match(/^-\d+\.$/)) 
			{
				this.value += '0';
				x.selectionEnd = this.value.length;
			}
			else if (this.value.match(/^-\d+$/)) 
			{
				this.value += '.0';
				x.selectionEnd = this.value.length;
			}
			o.value = this.value;
			o.value = o.value.replace(/^\D+/, '');
			o.value = o.value.replace(/[^\d\.]/g, function(){valueBack = true; return '';});
			
			main.toFloat.call(o);
			this.value = '-' + o.value;
			main.backSelectRange(this, x, valueBack);
		},
		toFloat : function(e){
			var x = new main.createSelectRange(this);
			var valueBack, o = {};
			if (this.value.match(/^\D+/))
			{
				this.value = '0.0';
				x.selectionEnd = this.value.length;
				valueBack = true;
			}
			else if (this.value.match(/^\d+\.$/)) 
			{
				this.value += '0';
				x.selectionEnd = this.value.length;
			}
			else if (this.value.match(/^\d+$/)) 
			{
				this.value += '.0';
				x.selectionEnd = this.value.length;
			}
			o.value = this.value;
			o.value = o.value.replace(/[^\d\.]/g, function(){valueBack = true; return '';});
			var value_cut = o.value.split(/\./);
			value_cut[0] = value_cut[0] || '0';
			value_cut[0] = value_cut[0].replace(/[^\d]|^0+(\d+)$/g, function(){valueBack = true; return arguments[1];});
			if (2 == value_cut.length)
			{
				var end = value_cut[1] || '';
				end = end.replace(/\D/g, function(){valueBack = true; return '';});
				o.value = value_cut[0] + '.' + (end || '0');
			}
			else if (2 < value_cut.length)
			{
				o.value = value_cut[0] + '.';
				for (var i=1,L=value_cut.length; i<L; i++)
				{
					var v = value_cut[i] || '';
					v = v.replace(/\D/g, function(){valueBack = true; return '';});
					o.value += v;
				}
			}
			this.value = o.value;
			main.backSelectRange(this, x, valueBack);
		},
		toW : function(e)
		{
			var x = new main.createSelectRange(this);
			var valueBack;
			this.value = this.value.replace(/[^\w]+/g, function(){valueBack = true; return '';});
			main.backSelectRange(this, x, valueBack);
		},
		upperCase : function(e)
		{
			var x = new main.createSelectRange(this);
			this.value = this.value.toUpperCase();
			main.backSelectRange(this, x);
		},
		lowerCase : function(e)
		{
			var x = new main.createSelectRange(this);
			this.value = this.value.toLowerCase();
			main.backSelectRange(this, x);
		},
		// 待測試
		toText : function(e)
		{
			var x = new main.createSelectRange(this);
			this.value = this.value.replace(/&/, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/\n/g, '<br />');
			main.backSelectRange(this, x);
		}
	};
})();